package com.easybuyzjf.web;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easybuyzjf.mapper.EbNewsMapper;
import com.easybuyzjf.model.EbNews;
import com.easybuyzjf.model.EbProduct;
import com.easybuyzjf.model.EbProductCategory;
import com.easybuyzjf.service.EbNewsService;
import com.easybuyzjf.service.EbProductCategoryService;
import com.easybuyzjf.service.EbProductService;
import org.apache.coyote.Request;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller


@RequestMapping("/index")
public class IndexController {

    @Autowired
    private EbProductService ebProductService;
    @Autowired
    private  EbNewsService ebNewsService;
    @Autowired
    private EbProductCategoryService ebProductCategoryService;


    @RequestMapping("/front")

    public String index(@RequestParam(defaultValue ="1") int psize,
                        @RequestParam(defaultValue = "8") int pageSize,
                        Model model, HttpServletRequest request
                        ) {
        //System.out.println("访问前台首页");
        //TODO 1. 查分页商品数据
        Page<EbProduct> productPage= ebProductService.list(psize,pageSize);

        Page<EbNews> newsPage=ebNewsService.list(psize,pageSize);

        Map<String, List<EbProductCategory>> cateMap= ebProductCategoryService.selectBigAndSmallCate();
        model.addAttribute("newsPage",newsPage);

        model.addAttribute("productPage",productPage);

        //model.addAttribute("cateMap",cateMap);
        request.getSession().setAttribute("cateMap",cateMap);

        return "index";
    }
}
